<template>
  <div class="main">
    <div class="main_head">录入酒企</div>
    <el-form :model="dataForm" :rules="rules" ref="dataForm" label-position="left" label-width="auto" class="dataForm">
      <div class="dataForm_flex">
        <el-form-item label="酒企名称" prop="name">
          <el-input class="form_item" maxlength="50" v-model="dataForm.name" placeholder="请输入酒企名称"></el-input>
        </el-form-item>
        <el-form-item label="法人" prop="legalPerson">
          <el-input class="form_item" maxlength="50" v-model="dataForm.legalPerson" placeholder="请输入法人"></el-input>
        </el-form-item>
      </div>
      <div class="dataForm_flex">
        <el-form-item label="联系人" prop="linkman">
          <el-input class="form_item" maxlength="50" v-model="dataForm.linkman" placeholder="请输入联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="contactWay">
          <el-input class="form_item" type="number" v-model="dataForm.contactWay" placeholder="请输入联系方式"></el-input>
        </el-form-item>
      </div>

      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">基本存款账户信息：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.basicAccount" />
          </div>
        </div>
        <div class="img_right">
          <div>
            <div class="red">*</div>
            <div class="text">营业执照：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.businessLicense" />
          </div>
        </div>
      </div>
      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">企业法人身份证(正面)：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.legalPersonCardF" />
          </div>
        </div>
        <div class="img_right">
          <div>
            <div class="red">*</div>
            <div class="text">企业法人身份证(反面)：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.legalPersonCardB" />
          </div>
        </div>

      </div>
      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">食品生产许可证：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.foodProductionPermit" />
          </div>
        </div>
        <div class="img_right">
          <div>
            <div class="red">*</div>
            <div class="text">商标注册证：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.tradeMarkLicense" />
          </div>
        </div>
      </div>
      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">销售授权书：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.marketingAuth" />
          </div>
        </div>
        <div class="img_right">
          <div>
            <div class="red">*</div>
            <div class="text">食品经营许可证：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.foodBusinessLicense" />
          </div>
        </div>
      </div>
      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">生产单位营业执照：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.licenseProductionUnit" />
          </div>
        </div>
        <div class="img_right">
          <div>
            <div class="red">*</div>
            <div class="text">质检报告：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.qualityInspectReport" />
          </div>
        </div>
      </div>
      <div class="dataForm_img">
        <div class="img_left">
          <div>
            <div class="red">*</div>
            <div class="text">一般纳税人资格证：</div>
          </div>
          <div class="img">
            <img-upload class="img-upload" v-model="dataForm.taxpayerQualiCert" />
          </div>
        </div>
      </div>
      <el-form-item label="是否含进口商品" prop="importedGoods">
        <el-select v-model="dataForm.importedGoods">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <div v-if="this.dataForm.importedGoods == 1">
        <div class="dataForm_img is_imported">
          <div class="img_left">
            <div>
              <div class="red">*</div>
              <div class="text">委托进口协议：</div>
            </div>
            <div class="img">
              <img-upload class="img-upload" v-model="dataForm.entrustedImportAgreement" />
            </div>
          </div>
          <div class="img_right">
            <div>
              <div class="red">*</div>
              <div class="text">报关单：</div>
            </div>
            <div class="img">
              <img-upload class="img-upload" v-model="dataForm.customsDeclaration" />
            </div>
          </div>
        </div>
        <div class="dataForm_img">
          <div class="img_left">
            <div>
              <div class="red">*</div>
              <div class="text">入镜检验检疫：</div>
            </div>
            <div class="img">
              <img-upload class="img-upload" v-model="dataForm.entryInspectQuarantine" />
            </div>
          </div>
        </div>
      </div>

      <el-form-item>
        <el-button v-if="this.dataForm.wineryId" type="primary" @click="submit('dataForm')">保存</el-button>
        <el-button v-else type="primary" @click="submitForm('dataForm')">确认</el-button>
        <el-button @click="back()">取消</el-button>
      </el-form-item>
    </el-form>
  </div>


</template>

<script>
  import * as api from '@/api/winery/winery'
  import ImgUpload from '@/components/ImgUpload'
  export default {
    components: {
      ImgUpload
    },
    data() {
      return {
        dataForm: {
          name: '',
          legalPerson: '',
          linkman: '',
          contactWay: '',
          basicAccount: '', //基础账户信息
          importedGoods: '',
          businessLicense: '',
          legalPersonCardF: '',
          legalPersonCardB: '',
          foodProductionPermit: '',
          tradeMarkLicense: '',
          marketingAuth: '',
          foodBusinessLicense: '',
          licenseProductionUnit: '',
          qualityInspectReport: '',
          taxpayerQualiCert: '',
          entrustedImportAgreement: '',
          customsDeclaration: '',
          entryInspectQuarantine: '',
          status: 1, //审核状态
          source: "后台录入", //入驻来源，
          wineryId: '',
          wineryStatus:1
        },
        rules: {
          name: [{
            required: true,
            message: '请输入酒企名称',
            trigger: 'blur'
          }],
          legalPerson: [{
            required: true,
            message: '请输入法人',
            trigger: 'blur'
          }],
          linkman: [{
            required: true,
            message: '请输入联系人',
            trigger: 'blur'
          }],
          contactWay: [{
            required: true,
            message: '请输入联系方式',
            trigger: 'blur'
          }],
          importedGoods: [{
            required: true,
            message: '请选择是否进口',
            trigger: 'change'
          }]
        }
      }
    },
    created() {
      // 获取上页面跳转路由参数
      console.log(this.$route.query)
      this.dataForm.wineryId = this.$route.query.wineryId
    },
    mounted() {
      this.getwineryDetail()
    },
    methods: {
      getwineryDetail() {
        if (this.dataForm.wineryId) {
          api.winery(this.dataForm.wineryId).then(data => {
            this.dataForm.name = data.name
            this.dataForm.legalPerson = data.legalPerson
            this.dataForm.linkman = data.linkman
            this.dataForm.contactWay = data.contactWay
            // this.dataForm.importedGoods = data.importedGoods
            this.dataForm.basicAccount = data.basicAccount
            this.dataForm.businessLicense = data.businessLicense
            this.dataForm.legalPersonCardF = data.legalPersonCardF
            this.dataForm.legalPersonCardB = data.legalPersonCardB
            this.dataForm.foodProductionPermit = data.foodProductionPermit
            this.dataForm.licenseProductionUnit = data.licenseProductionUnit
            this.dataForm.qualityInspectReport = data.qualityInspectReport
            this.dataForm.taxpayerQualiCert = data.taxpayerQualiCert
            this.dataForm.tradeMarkLicense = data.tradeMarkLicense
            this.dataForm.marketingAuth = data.marketingAuth
            this.dataForm.foodBusinessLicense = data.foodBusinessLicense
            this.dataForm.entrustedImportAgreement = data.entrustedImportAgreement
            this.dataForm.customsDeclaration = data.customsDeclaration
            this.dataForm.entryInspectQuarantine = data.entryInspectQuarantine
            this.dataForm.tradeMarkLicense = data.tradeMarkLicense
          })
        } else {
          console.log('进入wwwwwwwwwwwwwww加载页面数据')
        }

      },
      back() {
        this.$router.back()
      },
      submitForm(dataForm) {
        console.log(this.dataForm.name)
        console.log(this.dataForm.taxpayerQualiCert + '-*******************')
        this.$refs[dataForm].validate((valid) => {
          if (this.dataForm.basicAccount == '' || this.dataForm.businessLicense == '' || this.dataForm
            .legalPersonCardF == '' ||
            this.dataForm.legalPersonCardB == '' || this.dataForm.foodProductionPermit == '' || this.dataForm
            .licenseProductionUnit == '' ||
            this.dataForm.qualityInspectReport == '' || this.dataForm.taxpayerQualiCert == '' || this.dataForm
            .tradeMarkLicense == '' ||
            this.dataForm.marketingAuth == '' || this.dataForm.foodBusinessLicense == '') {
            console.log('是空的啊啊啊啊啊')
          } else if (this.dataForm.importedGoods == 1) {
            console.log('this.dataForm.importedGoods ========== 1')
            this.is_imp()
          } else if (this.dataForm.importedGoods !== 1) {
            console.log('this.dataForm.importedGoods !!!!!!!!!!!!!!!!!!!!= 1')
            this.save(dataForm)
          }
        });
      },
      //上传接口
      save(dataForm) {
        api.save(
          this.dataForm
        ).then(data => {
          this.$message({
            message: '录入成功！',
            type: 'success'
          });
          setTimeout(
            this.$router.push({
              path: '/winery/list'
            }), 1000)
        })

        console.log('成功了！！！！！！！！！');
      },
      //编辑 保存
      submit(dataForm) {
        this.$refs[dataForm].validate((valid) => {
          if (this.dataForm.basicAccount == '' || this.dataForm.businessLicense == '' || this.dataForm
            .legalPersonCardF == '' ||
            this.dataForm.legalPersonCardB == '' || this.dataForm.foodProductionPermit == '' || this.dataForm
            .licenseProductionUnit == '' ||
            this.dataForm.qualityInspectReport == '' || this.dataForm.taxpayerQualiCert == '' || this.dataForm
            .tradeMarkLicense == '' ||
            this.dataForm.marketingAuth == '' || this.dataForm.foodBusinessLicense == '') {
            console.log('是空的啊啊啊啊啊')
            this.$message({
              message: '信息上传不完整！',
              type: 'warning'
            });
          } else if (this.dataForm.importedGoods == 1 || this.dataForm.importedGoods == '') {
            if (this.dataForm.entrustedImportAgreement == '' ||
              this.dataForm.customsDeclaration == '' || this.dataForm.entryInspectQuarantine == '') {
              console.log('进口协议空')
              this.$message({
                message: '信息上传不完整！',
                type: 'warning'
              });
            } else {
              api.update(
                this.dataForm
              ).then(data => {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                setTimeout(
                  this.$router.push({
                      path: '/winery/list'

                    }), 1000)
              })
            }
          } else if (this.dataForm.importedGoods !== 1) {
            api.update(
              this.dataForm
            ).then(data => {
              this.$message({
                message: '编辑成功！',
                type: 'success'
              });
              setTimeout(
                this.$router.push({
                    path: '/winery/list'

                  }), 1000)
            })
          }
        });

      },
      //判断进口照片
      is_imp() {
        if (this.dataForm.entrustedImportAgreement == '' ||
          this.dataForm.customsDeclaration == '' || this.dataForm.entryInspectQuarantine == '') {
          console.log('进口协议空')
          this.$message({
            message: '信息上传不完整！',
            type: 'warning'
          });
        } else {
          this.save()
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  //输入数字
  ::v-deep input::-webkit-outer-spin-button,
  ::v-deep input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }

  ::v-deep input[type='number'] {
    -moz-appearance: textfield !important;
  }

  .main {
    margin: 30px 500px 50px 50px;

    .main_head {
      font-weight: bold;
      font-size: 20px;
      margin-bottom: 50px;
    }

    .dataForm {

      // 输入酒企信息
      .dataForm_flex {
        display: flex;
        justify-content: space-between;

        .form_item {
          width: 400px;
        }
      }

      // 上传照片
      .dataForm_img {
        margin-bottom: 50px;
        margin-right: 100px;
        display: flex;
        justify-content: space-between;

        .img_left {
          margin-right: 200px;
        }

        .red {
          color: red;
          padding-right: 3px;
          font-size: 14px;
          display: inline-block;
        }

        .text {
          display: inline-block;
          font-size: 14px;
          color: #606266
        }

        .img {
          display: inline-block;
          margin-left: 200px;

          .img-upload {
            ::v-deep .el-upload {
              background-color: #e0e1e1;

              .pic {
                display: block;
                width: 120px;
                height: 120px;
                padding: 2px;
                border-radius: 6px;
              }
            }
          }
        }
      }
    }
  }
</style>
